package edu.gyc.swing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class TestMenu extends JFrame {
    private JToolBar toolBar = new JToolBar();
    private JButton up = new JButton("up");
    private JButton down = new JButton("down");
    private JLabel label;
    private JFileChooser chooser;
    private static String[] currentPath = new String[10000];// 最多一万张图片
    private static int dirPhotoCount = 0;
    private static boolean flag = false; //user yes or not open dir

    public static void getFileNameToFullPhotoArray(String dirPath) {
        String path = dirPath;
        File f = new File(path);
        if (!f.exists()) {
            System.out.println(path + " not exists");
            return;
        }
        int cut = 0;
        File fa[] = f.listFiles();
        for (int i = 0; i < fa.length; i++) {
            File fs = fa[i];
            try {
                if (ImageIO.read(fs) != null) { // Yes or not photo style file.
                    currentPath[cut++] = dirPath + "\\" + fs.getName();
                }
            } catch (IOException ex) {

            }
        }
        dirPhotoCount = cut;
    }

    public TestMenu() {
        super();
        setTitle("PHOTO viewer");
        this.setLocationRelativeTo(null);
        getContentPane().add(toolBar, BorderLayout.NORTH);
        label = new JLabel();

        label.setBackground(Color.white);
        this.chooser = new JFileChooser();// java provide file selected
        chooser.setCurrentDirectory(new File("."));// view path

        JMenuBar jmb = new JMenuBar();
        setJMenuBar(jmb);
        JMenu fileMenu = new JMenu("File");
        JMenuItem jmiExit = new JMenuItem("Exit");
        JMenuItem jmiOpen = new JMenuItem("Open");
        jmb.add(fileMenu);
        toolBar.add(up);
        toolBar.add(down);
        getContentPane().add(toolBar, BorderLayout.NORTH);

         add(label);
        System.out.println("label:"+label.getSize());
        jmb.add(fileMenu);
        fileMenu.add(jmiOpen);
        fileMenu.add(jmiExit);



        up.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (flag) {
                    String dir = chooser.getSelectedFile().getParent();
                    getFileNameToFullPhotoArray(dir);// current dir build photo array
                    String currentImage = label.getIcon().toString();
                    for (int i = 0; i < dirPhotoCount; i++) {
                        if (currentPath[i].equals(currentImage)) {
                            label.setIcon(new ImageIcon(currentPath[i == 0 ? dirPhotoCount - 1 : i - 1]));
                            break;
                        }
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "当前无图片，请选择正确文件", "打开失败", JOptionPane.ERROR_MESSAGE);

                }
            }

        });
        down.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (flag) {
                    String dir = chooser.getSelectedFile().getParent();
                    getFileNameToFullPhotoArray(dir);// 对当前目录建立图片数组
                    String currentImage = label.getIcon().toString();
                    for (int i = 0; i < dirPhotoCount; i++) {
                        if (currentPath[i].equals(currentImage)) {
                            label.setIcon(new ImageIcon(currentPath[i == dirPhotoCount - 1 ? 0 : i + 1]));
                            break;
                        }
                    }
                } else {

                    JOptionPane.showMessageDialog(null, "当前无图片，请选择正确文件", "打开失败", JOptionPane.ERROR_MESSAGE);

                }
            }

        });

        jmiOpen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int result = chooser.showOpenDialog(null);
                if (result == JFileChooser.APPROVE_OPTION) {
                    try {
                        if (ImageIO.read(chooser.getSelectedFile()) != null) { // 判断是否是图片文件
                            flag = true;
                            String name = chooser.getSelectedFile().getPath();
                            label.setIcon(new ImageIcon(name));
                        } else {
                            JOptionPane.showMessageDialog(null, "请选择正确文件", "打开失败", JOptionPane.ERROR_MESSAGE);
                        }
                    } catch (IOException ex) {

                    }

                }
            }
        });

        jmiExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {// using Runnable anonymous
            // object
            public void run() {
                JFrame frame = new TestMenu();
                frame.setSize(800, 600);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);

            }

        });

    }

}